package org.example.algorithm.backtracking;

import java.util.ArrayList;
import java.util.List;

//78. 子集
//输入：nums = [1,2,3]
//输出：[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
public class SubsetsSolution {
    private List<List<Integer>> ans = new ArrayList<>();
    private List<Integer> subset = new ArrayList<>();

    public List<List<Integer>> subsets(int[] nums) {
        int len = nums.length;
        if (len == 0) {
            return ans;
        }
        dfs(nums, 0);
        return ans;
    }

    private void dfs(int[] nums, int index) {
        if (index == nums.length) {
            ans.add(new ArrayList<>(subset));
            return;
        }
        dfs(nums, index+1);
        subset.add(nums[index]);
        dfs(nums, index+1);
        subset.remove(subset.size()-1);
    }
}
